package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.model.dao.ProcurementOfGoodsMapper;
import com.sservice.model.dao.StoreAccountMapper;
import com.sservice.model.entity.ProcurementOfGoods;
import com.sservice.model.entity.StoreAccount;
import com.sservice.service.service.IStoreAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by suweiming on 2017/12/20.
 */

@Transactional
@Service
public class StoreAccountServiceImpl implements IStoreAccountService {

    @Autowired
    private StoreAccountMapper dao;
    @Autowired
    private ProcurementOfGoodsMapper pogDao;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(StoreAccount record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(StoreAccount record) {


        return dao.insertSelective(record);
    }

    @Override
    public StoreAccount selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public StoreAccount selectByStoreId(String storeId) {
        return dao.selectByStoreId(storeId);
    }

    @Override
    public int updateByPrimaryKeySelective(StoreAccount record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(StoreAccount record) {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * 增加仓库库存
     *
     * @param record
     * @return
     */
    @Override
    public Object plusNumber(StoreAccount record, String userid) {
        StoreAccount sa = dao.selectByStoreId(String.valueOf(record.getStoreId()));
        if (sa == null) {
            sa = new StoreAccount();
            sa.setStoreId(record.getStoreId());
            sa.setNumberCanSell(record.getNumberCanSell()); //增加库存
            dao.insertSelective(sa);
        } else {
            sa.setNumberCanSell(record.getNumberCanSell() + sa.getNumberCanSell());
            dao.updateByPrimaryKey(sa);
        }
        ProcurementOfGoods pof = new ProcurementOfGoods();
        pof.setNumber(record.getNumberCanSell());
        pof.setStoreId(record.getStoreId());
        pof.setUserId(userid);
        pof.setNo(Constants.generateServiceNO("P"));
        return pogDao.insertSelective(pof);
    }
}
